﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MediaLibrary.Controls;

using AMPLibrary.Collections;
using AMPLibrary.Media;
using AMPLibrary.Controllers;

namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for FavoritesPage.xaml
    /// </summary>
    public partial class PersonalPage : Page
    {
        public const int NUMBER_PINNED_TO_TAKE = 25;
        public const int NUMBER_FAVORITES_TO_TAKE = 25;
        public const int NUMBER_RECENTLYPLAYED_TO_TAKE = 25;
        public const int NUMBER_MOSTPLAYED_TO_TAKE = 25;

        // get the applications music collection
        MusicCollection musicCollection = MusicCollection.GetMusicCollection();

        // get the applications video collection
        VideoCollection videoCollection = VideoCollection.GetVideoCollection();

        // the singleton instance of the favorites page
        private static PersonalPage personalPage;

        /// <summary>
        /// constructor, private, hidden from public for use in singleton design pattern
        /// </summary>
        private PersonalPage()
        {
            InitializeComponent();

            PersonalDataTemplateSelector personalDataTemplateSelector = new PersonalDataTemplateSelector();

            this.highestRatedMusicListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.mostPlayedMusicListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.favoriteMusicListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.recentlyPlayedMusicListView.ItemTemplateSelector = personalDataTemplateSelector;

            this.highestRatedVideoListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.mostPlayedVideoListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.favoriteVideoListView.ItemTemplateSelector = personalDataTemplateSelector;
            this.recentlyPlayedVideoListView.ItemTemplateSelector = personalDataTemplateSelector;

            BindInformation();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindRecentlyPlayedMusic()
        {
            this.recentlyPlayedMusicListView.ItemsSource = (from m in musicCollection where m.LastPlayed != DateTime.MinValue orderby m.LastPlayed descending select m).Take(NUMBER_MOSTPLAYED_TO_TAKE);
            this.recentlyPlayedMusicListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindMostPlayedMusic()
        {
            this.mostPlayedMusicListView.ItemsSource = (from m in musicCollection where m.PlayCount != 0 orderby m.PlayCount descending select m).Take(NUMBER_MOSTPLAYED_TO_TAKE);
            this.mostPlayedMusicListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindFavoriteMusic()
        {
            this.favoriteMusicListView.ItemsSource = (from m in musicCollection where m.IsFavorited != false orderby m.UserMediaRating descending, m.PlayCount select m);
            this.favoriteMusicListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindHighestRatedMusic()
        {
            this.highestRatedMusicListView.ItemsSource = (from m in musicCollection where m.UserMediaRating != UserMediaRating.Unrated orderby m.UserMediaRating descending, m.PlayCount descending select m);
            this.highestRatedMusicListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindRecentlyPlayedVideo()
        {
            this.recentlyPlayedVideoListView.ItemsSource = (from v in videoCollection where v.LastPlayed != DateTime.MinValue orderby v.LastPlayed descending select v).Take(NUMBER_MOSTPLAYED_TO_TAKE);
            this.recentlyPlayedVideoListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindMostPlayedVideo()
        {
            this.mostPlayedVideoListView.ItemsSource = (from v in videoCollection where v.PlayCount != 0 orderby v.PlayCount descending select v).Take(NUMBER_MOSTPLAYED_TO_TAKE);
            this.mostPlayedVideoListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindFavoriteVideo()
        {
            this.favoriteVideoListView.ItemsSource = (from v in videoCollection where v.IsFavorited != false select v);
            this.favoriteVideoListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindHighestRatedVideo()
        {
            this.highestRatedVideoListView.ItemsSource = (from v in videoCollection where v.UserMediaRating != UserMediaRating.Unrated orderby v.UserMediaRating descending select v);
            this.highestRatedVideoListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        public void BindInformation()
        {
            WatchFolderController wfc = WatchFolderController.GetWatchFolderController();

            wfc.PauseMusic = true;
            wfc.PausePlaylist = true;
            wfc.PauseVideo = true;

            System.Threading.Thread.Sleep(250);

            BindMostPlayedMusic();
            BindFavoriteMusic();
            BindRecentlyPlayedMusic();
            BindHighestRatedMusic();

            BindMostPlayedVideo();
            BindFavoriteVideo();
            BindRecentlyPlayedVideo();
            BindHighestRatedVideo();

            wfc.PauseMusic = false;
            wfc.PausePlaylist = false;
            wfc.PauseVideo = false;
        }

        /// <summary>
        /// Singleton Constructor, gets the current instance of the personal page
        /// </summary>
        /// <returns></returns>
        public static PersonalPage GetPersonalPage()
        {
            if (personalPage == null)
                personalPage = new PersonalPage();

            return personalPage;
        }
     
        /// <summary>
        /// event handler, fired when the user wants to refresh the information on the personal page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            BindInformation();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.mostPlayedMusicListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.mostPlayedMusicListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedMusicListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.mostPlayedMusicListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedMusicListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.mostPlayedMusicListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }
        /******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.recentlyPlayedMusicListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.recentlyPlayedMusicListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedMusicListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.recentlyPlayedMusicListView.SelectedItems)
                    playlistMedia.AddItem(selectedMedia);

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedMusicListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.recentlyPlayedMusicListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.favoriteMusicListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.favoriteMusicListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteMusicListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.favoriteMusicListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteMusicListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.favoriteMusicListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

/******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.highestRatedMusicListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedMusicListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.highestRatedMusicListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedMusicListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.highestRatedMusicListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedMusicListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.highestRatedMusicListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pinnedMusicListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.highestRatedMusicListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.highestRatedMusicListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void favoriteMusicListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.favoriteMusicListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.favoriteMusicListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void recentlyPlayedMusicListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.recentlyPlayedMusicListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.recentlyPlayedMusicListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mostPlayedMusicListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.mostPlayedMusicListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.mostPlayedMusicListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /********************* VIDEO ***********************/

        /********************* VIDEO ***********************/

        /********************* VIDEO ***********************/

        /********************* VIDEO ***********************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingVideoResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.mostPlayedVideoListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingVideoResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.mostPlayedVideoListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistVideoResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedVideoListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.mostPlayedVideoListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistVideoResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.mostPlayedVideoListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.mostPlayedVideoListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingVideoResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.recentlyPlayedVideoListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingVideoResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.recentlyPlayedVideoListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistVideoResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedVideoListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.recentlyPlayedVideoListView.SelectedItems)
                    playlistMedia.AddItem(selectedMedia);

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistVideoResult2_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.recentlyPlayedVideoListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.recentlyPlayedVideoListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingVideoResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.favoriteVideoListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingVideoResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.favoriteVideoListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistVideoResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteVideoListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.favoriteVideoListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistVideoResult3_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.favoriteVideoListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.favoriteVideoListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /******************************************************/

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingVideoResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.highestRatedVideoListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingVideoResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedVideoListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.highestRatedVideoListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistVideoResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedVideoListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.highestRatedVideoListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistVideoResult4_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.highestRatedVideoListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.highestRatedVideoListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pinnedVideoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.highestRatedVideoListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.highestRatedVideoListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void favoriteVideoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.favoriteVideoListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.favoriteVideoListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void recentlyPlayedVideoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.recentlyPlayedVideoListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.recentlyPlayedVideoListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mostPlayedVideoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.mostPlayedVideoListView.SelectedItem == null)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            MediaFile media = this.mostPlayedVideoListView.SelectedItem as MediaFile;

            if (media == null)
                return;

            npc.AddItem(media);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }
    }
}
